home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / ccs_util.jar / com / commerceone / util / queue / Queue.class (.txt) next >
Encoding:
Java Class File  |  1999-12-09  |  2.4 KB  |  87 lines

  1. package com.commerceone.util.queue;
  2.  
  3. import com.commerceone.util.contract.Contract;
  4. import java.io.Serializable;
  5. import java.util.Enumeration;
  6. import java.util.Vector;
  7.  
  8. public class Queue implements Serializable {
  9.    public static final int DEFAULT_Q_SIZE = 128;
  10.    protected int frontIndex = 0;
  11.    protected int nextAvail = 0;
  12.    protected int queueLen = 0;
  13.    protected Vector vec = null;
  14.  
  15.    public Queue(int size) {
  16.       this.vec = new Vector();
  17.       this.vec.setSize(size);
  18.       this.queueLen = size;
  19.    }
  20.  
  21.    public synchronized Object peek() throws QueueEmptyException {
  22.       if (this.isEmpty()) {
  23.          throw new QueueEmptyException("Queue is empty");
  24.       } else {
  25.          return this.vec.elementAt(this.frontIndex);
  26.       }
  27.    }
  28.  
  29.    public Enumeration getElements() {
  30.       return this.getElements(this.vec.size());
  31.    }
  32.  
  33.    public synchronized int size() {
  34.       int sz = (this.queueLen - this.frontIndex + this.nextAvail) % this.queueLen;
  35.       Contract.ensure(sz >= 0);
  36.       return sz;
  37.    }
  38.  
  39.    public Enumeration getElements(int numEntries) {
  40.       Vector v = new Vector();
  41.       Object obj = null;
  42.       Enumeration enum = this.vec.elements();
  43.  
  44.       int i;
  45.       for(i = 0; i < numEntries && enum.hasMoreElements(); ++i) {
  46.          obj = enum.nextElement();
  47.          if (obj != null) {
  48.             v.addElement(obj);
  49.          } else {
  50.             --i;
  51.          }
  52.       }
  53.  
  54.       v.setSize(i);
  55.       return v.elements();
  56.    }
  57.  
  58.    // $FF: renamed from: dq () java.lang.Object
  59.    public synchronized Object method_0() throws QueueEmptyException {
  60.       Object tmp = null;
  61.       if (this.isEmpty()) {
  62.          throw new QueueEmptyException("Queue is empty");
  63.       } else {
  64.          tmp = this.vec.elementAt(this.frontIndex);
  65.          this.vec.setElementAt((Object)null, this.frontIndex);
  66.          this.frontIndex = (this.frontIndex + 1) % this.queueLen;
  67.          Contract.ensure(this.frontIndex >= 0);
  68.          return tmp;
  69.       }
  70.    }
  71.  
  72.    // $FF: renamed from: q (java.lang.Object) void
  73.    public synchronized void method_1(Object entry) throws QueueFullException {
  74.       if (this.size() == this.queueLen - 1) {
  75.          throw new QueueFullException("Queue is full");
  76.       } else {
  77.          this.vec.setElementAt(entry, this.nextAvail);
  78.          this.nextAvail = (this.nextAvail + 1) % this.queueLen;
  79.          Contract.ensure(this.nextAvail >= 0);
  80.       }
  81.    }
  82.  
  83.    public synchronized boolean isEmpty() {
  84.       return this.frontIndex == this.nextAvail;
  85.    }
  86. }
  87.